package com.xiaoshuidi.cloud.module.rooms.controller.admin.roomdevice;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xiaoshuidi.cloud.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.iot.api.device.DeviceApi;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.pricing.vo.HousePricingExportResp;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomdevice.vo.DepRoomVO;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomdevice.vo.RoomDeviceVO;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomdevice.vo.RoomSdiotPayModeConfigVO;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomdevice.vo.RoomSdiotPayModeMappingVO;
import com.xiaoshuidi.cloud.module.rooms.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomSdiotDeviceMapping;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomSdiotPayModeConfig;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomSdiotReadingOrder;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomSdiotReadingOrderRecord;
import com.xiaoshuidi.cloud.module.rooms.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Marker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "房源设备接口")
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(ApiConstants.NAME + "/roomdevice")
public class RoomDeviceController {

    private final RoomDeviceService roomDeviceService;

    private final RoomSdiotDeviceMappingService roomSdiotDeviceMappingService;

    private final RoomSdiotPayModeConfigService roomSdiotPayModeConfigService;

    private final RoomSdiotPayModeMappingService roomSdiotPayModeMappingService;

    private final RoomSdiotReadingOrderService roomSdiotReadingOrderService;

    private final RoomSdiotReadingOrderRecordService roomSdiotReadingOrderRecordService;
    @Autowired
    private DeviceApi deviceApi;


    /**
     * 关联设备分页列表
     *
     * @param pageSize
     * @param pageNum
     * @param apartmentId
     * @return
     */
    @GetMapping("/pageList")
    @Operation(summary = "分页查询")
    @Parameters({
            @Parameter(name = "pageSize", description = "每页大小"),
            @Parameter(name = "pageNum", description = "第几页"),
            @Parameter(name = "apartmentId", description = "公寓id"),
            @Parameter(name = "area", description = "小区"),
            @Parameter(name = "buildingNo", description = "栋"),
            @Parameter(name = "unitNo", description = "单元"),
            @Parameter(name = "houseNo", description = "小区房间"),
            @Parameter(name = "isConcentrated", description = "是否集中式")
    })
    public CommonResult<PageResult<RoomDeviceVO>> pageList(Integer pageSize, Integer pageNum, Long apartmentId, String area, String buildingNo, String unitNo, String houseNo, Boolean isConcentrated) {
        PageResult<RoomDeviceVO> roomDeviceVOPageResult = roomDeviceService.pageList(pageSize, pageNum, apartmentId, area, buildingNo, unitNo, houseNo, isConcentrated);
        return CommonResult.success(roomDeviceVOPageResult);
    }

    /**
     * 房源设备绑定
     *
     * @param roomSdiotDeviceMapping
     * @return
     */
    @PostMapping("/binding")
    @Operation(summary = "房源设备绑定")
    public CommonResult<String> bindingDeviceRoom(@RequestBody RoomSdiotDeviceMapping roomSdiotDeviceMapping) {
        CommonResult<String> commonResult;
        try {
            commonResult = roomSdiotDeviceMappingService.bindingDeviceRoom(roomSdiotDeviceMapping);
        } catch (Exception e) {
            log.error(Marker.ANY_MARKER, e);
            commonResult = CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), e.getMessage());
        }
        return commonResult;
    }

    /**
     * 取消关联
     *
     * @param deviceThirdNo
     * @param roomId
     * @return
     */
    @GetMapping("/unbinding")
    @Parameters({
            @Parameter(name = "deviceThirdNo", description = "设备编号"),
            @Parameter(name = "roomId", description = "房间号")
    })
    public CommonResult<String> unbindingDeviceRoom(String deviceThirdNo, Long roomId) {

        try {
            //设备相关操作
            RoomSdiotDeviceMapping deviceMapping = roomSdiotDeviceMappingService.getDeviceMapping(deviceThirdNo);
            CommonResult<String> stringCommonResult = roomSdiotDeviceMappingService.unbindingDeviceRoom(deviceThirdNo, roomId);
            log.info("取消关联操作结果："+ JSONObject.toJSONString(stringCommonResult));
            if(String.valueOf(stringCommonResult.getCode()).equals("0")){
                deviceApi.unbind(deviceMapping.getDeviceNo());
            }
        } catch (Exception e) {
            log.error(Marker.ANY_MARKER, e);
            return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), "取消管理失败");
        }
        return CommonResult.success("取消关联成功");

    }

    /**
     * 添加设备付费模式
     *
     * @param roomSdiotPayModeConfig
     * @return
     */
    @PostMapping("addRoomSdiotPayMode")
    public CommonResult<String> addRoomSdiotPayMode(@RequestBody RoomSdiotPayModeConfig roomSdiotPayModeConfig) {
        return roomSdiotPayModeConfigService.addRoomSdiotPayMode(roomSdiotPayModeConfig);
    }

    /**
     * 付费模式应用到房源
     *
     * @param
     * @return
     */
    @PostMapping("addRoomSdiotPayModeMapping")
    public CommonResult<String> saveRoomSdiotPayMode(@RequestBody RoomSdiotPayModeMappingVO roomSdiotPayModeMappingVO) {
        return roomSdiotPayModeMappingService.saveRoomSdiotPayMode(roomSdiotPayModeMappingVO);
    }


    /**
     * 查询付费模式列表
     *
     * @return
     */
    @GetMapping("roomSdiotPayMode/pageList")
    public CommonResult<List<RoomSdiotPayModeConfigVO>> findAllPayMode() {
        List<RoomSdiotPayModeConfigVO> list = roomSdiotPayModeConfigService.findAll();
        return CommonResult.success(list);
    }

    /**
     * 删除付费模式
     *
     * @param roomSdiotPayModeConfig
     * @return
     */
    @PostMapping("roomSdiotPayMode/delete")
    public CommonResult<String> deletePayMode(@RequestBody RoomSdiotPayModeConfig roomSdiotPayModeConfig) {
        return roomSdiotPayModeConfigService.delete(roomSdiotPayModeConfig);
    }

    /**
     * 回显对应付费模式的房间号
     *
     * @param payModeConfigId
     * @return
     */
    @GetMapping("roomSdiotPayMode/houseRoomId")
    public CommonResult<List<DepRoomVO>> findPayModeHouseRoomId(@Parameter long payModeConfigId) {
        List<DepRoomVO> list = roomSdiotPayModeMappingService.findPayModeHouseRoomId(payModeConfigId);
        return CommonResult.success(list);
    }

    /**
     * 批量抄表页面展示
     *
     * @param pageSize
     * @param pageNum
     * @param feeType
     * @param roomName
     * @return
     */
    @GetMapping("readingOredr/pagelist")
    @Parameters({
            @Parameter(name = "pageSize", description = "每页大小"),
            @Parameter(name = "pageNum", description = "第几页"),
            @Parameter(name = "feeType", description = "费项"),
            @Parameter(name = "roomName", description = "房源名称"),
            @Parameter(name = "isConcentrated", description = "是否集中式")
    })
    public CommonResult<IPage<RoomSdiotReadingOrder>> readingOrderPageList(Integer pageSize, Integer pageNum, String feeType, String roomName, Boolean isConcentrated) {
        // todo @James 每个业务要定义独立的vo，只返回相应的属性, 不要将数据库实体直接返回
        IPage<RoomSdiotReadingOrder> pageResult = roomSdiotReadingOrderService.readingOrderPageList(pageSize, pageNum, feeType, roomName, isConcentrated);
        return CommonResult.success(pageResult);
    }

    /**
     * 保存抄表记录
     *
     * @param list
     * @return
     */
    @PostMapping("readingOrderRecord/save")
    public CommonResult<String> saveReadingOrderRecord(@RequestBody List<RoomSdiotReadingOrderRecord> list) {
        return roomSdiotReadingOrderRecordService.saveReadingOrderRecord(list);
    }

    /**
     * 查询抄表历史记录
     *
     * @param pageSize
     * @param pageNum
     * @param readingOrderId
     * @return
     */
    @GetMapping("readingOrderRecord/getRecord")
    @Parameters({
            @Parameter(name = "pageSize", description = "每页大小"),
            @Parameter(name = "pageNum", description = "第几页"),
            @Parameter(name = "readingOrderId", description = "抄表单ID")
    })
    public CommonResult<PageResult<RoomSdiotReadingOrderRecord>> getRecord(Integer pageSize, Integer pageNum, Long readingOrderId) {
        // todo @黄光辉 每个业务要定义独立的vo，只返回相应的属性, 不要将数据库实体直接返回
        PageResult<RoomSdiotReadingOrderRecord> pageResult = roomSdiotReadingOrderRecordService.getRecord(pageSize, pageNum, readingOrderId);
        return CommonResult.success(pageResult);

    }

    /**
     * 批量抄表页面导出
     *
     * @return
     */
    @GetMapping("readingOrderExport")
    @Parameters({
            @Parameter(name = "pageSize", description = "每页大小"),
            @Parameter(name = "pageNum", description = "第几页"),
            @Parameter(name = "feeType", description = "费项"),
            @Parameter(name = "roomName", description = "房源名称"),
            @Parameter(name = "isConcentrated", description = "是否集中式")
    })
    public CommonResult<HousePricingExportResp> readingOrderExport(Integer pageSize, Integer pageNum, String feeType, String roomName, Boolean isConcentrated) {
        HousePricingExportResp housePricingExportResp = roomSdiotReadingOrderService.readingOrderExport(pageSize, pageNum, feeType, roomName, isConcentrated);
        return CommonResult.success(housePricingExportResp);
    }

    /**
     * 批量抄表页面导入
     *
     * @param fileUrl
     * @return
     */
    @PostMapping("readingOrderImport")
    public CommonResult<String> readingOrderImport(@Parameter String fileUrl) {
        CommonResult<String> result = roomSdiotReadingOrderService.readingOrderImport(fileUrl);
        return result;
    }


}
